﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orion.Models;

namespace Orion.Controllers
{
    public class NewsArticleController : Controller
    {
        //unittest
        private NewsArticleRepositry m_rep = new NewsArticleRepositry();
        private int displayNewsArticles = 10;
        private NewsArticleRepositry m_newsArticleRep = new NewsArticleRepositry();
        private HomeIndexViewModel m_article = new HomeIndexViewModel();

        public ActionResult GetAllArticles( int? id )
        {
            //validation
            if ( ! id.HasValue )
            {
                return View( "Error" );
            }

			//all active articles fetched
            var allNewsArticles = m_rep.GetAllNewsArticles();
            m_article.NewsArticle = from a in allNewsArticles
                                    where a.Valid == true
                                    && a.ContentType == 1
                                    select a;

            m_article.counter = m_article.NewsArticle.Count();
			//nr of articles per page set
            m_article.displayNewsArticle = displayNewsArticles;
			
			//nr of pages calculated
            var n = m_article.counter;
            m_article.counter = n / displayNewsArticles;
            if (n % displayNewsArticles != 0)
            {
                m_article.counter++;
            }

            return View( m_article );
        }

		public ActionResult GetAllNews( int? id )
		{
			//validation
			if ( ! id.HasValue )
			{
				return View( "Error" );
			}

			//all actie news fetched
            var allNewsArticles = m_rep.GetAllNewsArticles( );
            m_article.NewsArticle = from a in allNewsArticles
                                    where a.Valid == true
                                    && a.ContentType == 2
                                    select a;

            m_article.counter = m_article.NewsArticle.Count( );

			//nr of articles per page set
            m_article.displayNewsArticle = displayNewsArticles;
			//nr of pages calculated
            var n = m_article.counter;
            m_article.counter = n / displayNewsArticles;
            if ( n % displayNewsArticles != 0 )
            {
                m_article.counter++;
            }

            return View( m_article );
		}

        [ValidateInput(false)]
        [HttpGet]
        public ActionResult NewsArticleById( int? id )
        {
			//validation
            if( ! id.HasValue )
            {
				return View( "Error" );
            }
            
            var temp = m_newsArticleRep.GetNewsArticleById( id.Value );

			//articles / news after same auther as the selected one fetched
            m_article.NewsArticle = from na in m_newsArticleRep.GetAllNewsArticles()
                                    where na.Author == temp.Author && ( na.ContentType == 1 || na.ContentType == 2)
                                    && na.ID != temp.ID
                                    select na;
                        
			m_article.MainNewsArticle = temp;

			return View( m_article );
        }

		public ActionResult PrintNewsArticleById( int? id )
		{
			//validation
            if( ! id.HasValue )
            {
				return View( "Error" );
            }
            
            var temp = m_newsArticleRep.GetNewsArticleById( id.Value );

			//articles / news after same auther as the selected one fetched
			m_article.NewsArticle = from na in m_newsArticleRep.GetAllNewsArticles()
									where na.Author == temp.Author
									&& na.ID != temp.ID
									select na;

			m_article.MainNewsArticle = temp;

			return View( m_article );
		}
	}
}
